//
//  WebInfo_GetView_Task_Cache.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-2-13.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "WebInfo_GetView_Task_Cache.h"
#import "EpointWebInfoModel.h"

@implementation WebInfo_GetView_Task_Cache

-(void)execute
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *ValidateData = [EpointBizTools getToken];
    NSString *url = self.getMobileOAWebserviceURL;
    NSString *wsNamespace = NSLocalizedString(@"Epoint_DNet_WS_NameSpace", @"");
    NSString *userguid = [userDefaults objectForKey:Epoint_User_UserGuid];
    NSString *pagesize = [params objectForKey:@"pagesize"];
    NSString *keyword = [params objectForKey:@"keyword"];
    NSString *categoryguid = [params objectForKey:@"categoryguid"];
    NSString *CurrentPageIndex = [params objectForKey:@"CurrentPageIndex"];
    
    
    NSString *ParasXml = @"<![CDATA[<?xml version=\"1.0\" encoding=\"gb2312\"?><paras><UserGuid>%@</UserGuid><KeyWord>%@</KeyWord><CurrentPageIndex>%@</CurrentPageIndex><PageSize>%@</PageSize><CategoryGuid>%@</CategoryGuid></paras>]]>";
    
    keyword = [EpointStringUtil filterXmlString:keyword];
    ParasXml = [NSString stringWithFormat:ParasXml,userguid,keyword,CurrentPageIndex,pagesize,categoryguid];
    
    
    //开始请求,判断网络是否可用
    if (![self isNetWorkAvailable])
    {
        [self userCache:ParasXml message:@"网络请求失败，请检查网络配置！"];
    }
    else
    {
        //请求接口
        EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:wsNamespace method:@"WebInfo_GetView"];
        [ws addParameterForString:@"ValidateData" value:ValidateData];
        [ws addParameterForString:@"ParasXml" value:ParasXml];
        NSString *bs =  ws.start;
        
        NSMutableArray *result = [self validateXMLWS2:bs];
        NSString *msg = [result objectAtIndex:1];
        if ([[result objectAtIndex:0]isEqualToString:@"1"]) {
            [DBFrameService setCacheValue:ParasXml value:bs];
            [self exeSuccessWithData:[self getLst:bs]];
        }
        else
        {
            [self userCache:ParasXml message:msg];
        }
    }
}

-(void)userCache:(NSString *)key message:(NSString *)msg
{
    //获取缓存数据
    NSString *cacheBS = [DBFrameService getCacheValue:key];
    
    //判断是否存在缓存
    if (cacheBS.length>0) {
        [self exeSuccessWithData:[self getLst:cacheBS]];
    }
    else
    {
        [self exeFailureWithMessage:msg];
    }
}

-(NSMutableArray *)getLst:(NSString *)bs
{
    NSString *xmldata = [EpointStringUtil getXMLFormatedAttributeBsWithTag:bs xmlTag:@"InfoList"];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmldata  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"Info"];
    NSMutableArray *listmp = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays)
    {
        EpointWebInfoModel *m = [[EpointWebInfoModel alloc]init];
        GDataXMLElement *InfoGuid = [[arr elementsForName:@"InfoGuid"] objectAtIndex:0];
        m.InfoGuid = [InfoGuid stringValue];
        
        GDataXMLElement *txtTitle = [[arr elementsForName:@"txtTitle"] objectAtIndex:0];
        m.txtTitle = [txtTitle stringValue];
        
        GDataXMLElement *Content = [[arr elementsForName:@"Content"] objectAtIndex:0];
        m.Content = [Content stringValue];
        
        GDataXMLElement *PostUserName = [[arr elementsForName:@"PostUserName"] objectAtIndex:0];
        m.PostUserName = [PostUserName stringValue];
        
        GDataXMLElement *PostDate = [[arr elementsForName:@"PostDate"] objectAtIndex:0];
        m.PostDate = [PostDate stringValue];
        
        GDataXMLElement *InfoProperty = [[arr elementsForName:@"InfoProperty"] objectAtIndex:0];
        m.InfoProperty = [InfoProperty stringValue];
        
        GDataXMLElement *IsRead = [[arr elementsForName:@"IsRead"] objectAtIndex:0];
        m.IsRead = [IsRead stringValue];
        
        GDataXMLElement *HasAttach = [[arr elementsForName:@"HasAttach"] objectAtIndex:0];
        m.HasAttach = [HasAttach stringValue];
        
        GDataXMLElement *FeedBackCount = [[arr elementsForName:@"FeedBackCount"] objectAtIndex:0];
        m.FeedBackCount = [FeedBackCount stringValue];
        
        [listmp addObject:m];
    }
    
    return listmp;
}

@end
